/**
 * 地区三级级联选择
 * @param $
 */(function($) {
	$.fn.region = function(options) {
        var opts = $.extend({}, $.fn.region.defaults, options);
        return this.each(function() {
            var _$this = $(this);
            var _$sheng = $("<select name ='shengid' id='shengid' class='myselect' ></select>");
            var _$shi = $("<select name ='shiid' id='shiid' class='myselect'></select>");
            var _$di = $("<select name ='diid' id='diid' class='myselect'></select>");
            _$this.append(_$sheng).append(_$shi).append(_$di);
            $.fn.region.loadElement(_$sheng,0,-1);
            _$sheng.on("change",function(){
            	$.fn.region.loadElement(_$shi,_$sheng.val(),-1);
            	_$shi.on("change",function(){
            		$.fn.region.loadElement(_$di,_$shi.val(),-1);
            	});
            });
            
        });
    };
    $.fn.region.loadElement = function(element,fid,selectedid) {
        $.ajax({
            url :_ctx+"/admin/resource/region/byfid/"+fid,
            type : 'get',
            data : {
            },
            datatype : "json",
            success : function(returned){
            	if(returned.success)
            	{
            		var data = returned.data;
            		var len =data.length;
            		$(element).html("");
            		$(element).append("<option value='-1'>-请选择-</option>");
            		for(var i=0;i<len;i++)
            		{
            			if(data[i].code==selectedid)
            			{
            				$(element).append("<option value='"+data[i].code+"' selected='selected'>"+data[i].name+"</option>");
            			}
            			else
            			{
            				$(element).append("<option value='"+data[i].code+"'>"+data[i].name+"</option>");
            			}
            			
            		}
            	}
            },
            erorr : function(request, erorr, exp) {
                alert(erorr);
            }
        });
    };
    $.fn.region.defaults ={
    }
})(jQuery);